package com.human.server.controller;


import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.human.server.entity.Employees;
import com.human.server.model.R;
import com.human.server.model.request.EmployeesRequest;
import com.human.server.service.impl.EmployeesServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/human/employees")
public class EmployeesController {

    @Autowired
    private EmployeesServiceImpl employeesService;

    /**
     * 分页
     * @param request
     * @return
     */
    @RequestMapping("/page")
    public R page(@RequestBody EmployeesRequest request) {
        return R.success(employeesService.page(request, new LambdaQueryWrapper<Employees>()
                .and(StrUtil.isNotEmpty(request.getKeyword()),wrapper -> wrapper.like(Employees::getName, request.getKeyword()).or().eq(Employees::getEmployeeId, request.getKeyword()))
                .eq(request.getDepartmentId() != null, Employees::getDepartmentId, request.getDepartmentId())
                .eq(StrUtil.isNotEmpty(request.getStatus()), Employees::getStatus, request.getStatus())));
    }

    /**
     * 新增
     * @param employees
     * @return
     */
    @RequestMapping("/add")
    public R add(@RequestBody Employees employees) {
        return R.success(employeesService.save(employees));
    }

    /**
     * 更新
     * @param employees
     * @return
     */
    @RequestMapping("/update")
    public R update(@RequestBody Employees employees) {
        return R.success(employeesService.updateById(employees));
    }

    /**
     * 删除
     * @param employees
     * @return
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Employees employees) {
        return R.success(employeesService.removeById(employees));
    }

    /**
     * 详情
     * @param employees
     * @return
     */
    @RequestMapping("/detail")
    public R detail(@RequestBody Employees employees) {
        return R.success(employeesService.getById(employees.getId()));
    }

    /**
     * 列表
     * @return
     */
    @RequestMapping("/list")
    public R list() {
        return R.success(employeesService.list());
    }

    /**
     * 登录
     * @param employees
     * @return
     */
    @RequestMapping("/login")
    public R login(@RequestBody Employees employees) {

        return employeesService.login(employees);
    }

    /**
     * 退出登录
     * @return
     */
    @RequestMapping("/logout")
    public R logout() {
        StpUtil.logout();
        return R.success();
    }

}
